﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class ucSelectProduct : UserControl
    {
        public delegate void delegateProductSelected(List<int> lstProductID, List<string> lstProductName, List<string> lstProductEAN, List<double[]> lstGiaBan, List<int> lstNhaSX);
        public event delegateProductSelected evnProductSelected;

        public ucSelectProduct()
        {
            InitializeComponent();
            
            txtSearch.Focus();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                string strSearch = txtSearch.Text.Trim();

                //if (clsCommon.fncIsBlank(strSearch)) return;

                DataTable objData = clsDbAccess.fncSearchSanPham(Program.gobjDb, "", strSearch, clsCommon.fncCnvNullToInt(rdHienThi.EditValue));
                xAddSelectColumn(objData);
                grdControlSanPham.DataSource = objData;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private bool xAddSelectColumn(DataTable objData)
        {
            bool blnResult = false;
            try
            {
                if (objData == null) return false;
                objData.Columns.Add("chon", typeof(bool));

                blnResult = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn chọn sản phẩm này?")) return;

                Cursor.Current = Cursors.WaitCursor;

                List<int> lstProductID = new List<int>();
                List<string> lstProductName = new List<string>();
                List<string> lstProductEAN = new List<string>();
                List<double[]> lstGiaBan = new List<double[]>();
                List<int> lstNhaSX = new List<int>();

                int intProID;
                string strProName;
                string strProEAN;
                bool blnSelected;
                double[] dblGiaBan;
                int intNhaSX;

                //doc id va name
                for (int i = dgvSanPham.RowCount - 1; i >= 0; i--)
                {
                    blnSelected = clsCommon.fncCnvNullToBool(dgvSanPham.GetRowCellValue(i, colChon));
                    if (!blnSelected) continue;

                    intProID = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(i, colProduct_id));
                    strProName = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(i, colPro_name));
                    strProEAN = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(i, colProduct_ean));

                    dblGiaBan = new double[6];
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_GOC] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_goc));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_LE] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_ban_le));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_cap1));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_cap2));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_CAP3] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_cap3));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.KHUYEN_MAI] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colKhuyenMai));

                    intNhaSX = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(i, colProduct_manufacturer_id));

                    lstProductID.Add(intProID);
                    lstProductName.Add(strProName);
                    lstProductEAN.Add(strProEAN);
                    lstGiaBan.Add(dblGiaBan);
                    lstNhaSX.Add(intNhaSX);

                    dgvSanPham.DeleteRow(i);
                }

                //phat ra su kien
                evnProductSelected(lstProductID, lstProductName, lstProductEAN, lstGiaBan, lstNhaSX);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar != (char)Keys.Enter) return;

                btnSearch_Click(null, null);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}
